package com.jinzhi.jzweb.controller;


import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

import com.jinzhi.common.utils.ApiResult;
import com.jinzhi.jzweb.domain.CertificationBatchDO;
import com.jinzhi.jzweb.domain.cond.InformationBatchCond;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.InformationBatchDO;
import com.jinzhi.jzweb.service.InformationBatchService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 认证批次
 * </pre>
 * <small> 2024-06-06 14:58:39 | Long</small>
 */
@Controller
@RequestMapping("/jzweb/informationBatch")
public class InformationBatchController extends BaseController {
	@Autowired
	private InformationBatchService informationBatchService;
	@Autowired
	private UserService userService;

	@GetMapping()
//	@RequiresPermissions("jzweb:informationBatch:informationBatch")
	String InformationBatch(){
	    return "jzweb/informationBatch/informationBatch";
	}

	@ResponseBody
	@GetMapping("/list")
//	@RequiresPermissions("jzweb:informationBatch:informationBatch")
	public Result<Page<InformationBatchDO>> list(InformationBatchDO informationBatchDTO){
		Wrapper<InformationBatchDO> wrapper = new EntityWrapper<InformationBatchDO>().orderBy("id", false);
		if (informationBatchDTO.getState() != null){
			wrapper.eq("state",informationBatchDTO.getState());
		}
		if (informationBatchDTO.getProfileName() != null){
			wrapper.like("profile_name","%"+informationBatchDTO.getProfileName()+"%");
		}
		// 查询列表数据
		Page<InformationBatchDO> page = informationBatchService.selectPage(getPage(InformationBatchDO.class), wrapper);

		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			InformationBatchDO obj = (InformationBatchDO) page.getRecords().get(i);
			if (obj.getCreaterId() != null) {
				UserDO userDO = userService.selectById(obj.getCreaterId());
				if (userDO != null) {
					obj.setCreaterName(userDO.getUsername());
				}
			}
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}

	@GetMapping("/selInformationBatchs")
	@ResponseBody
	public Result<?> selCertificateDetails() {
		Date nowDate = new Date();
		EntityWrapper<InformationBatchDO> wrapper = new EntityWrapper<InformationBatchDO>();
		wrapper.le("start_time",nowDate);
		wrapper.ge("end_time",nowDate);
		List<InformationBatchDO> certificationBatchDOS = informationBatchService.selectList(wrapper);

		if(certificationBatchDOS.isEmpty()){
			return Result.fail("暂无申报批次");
		}

		Map<String, Object> map1 = new HashMap<>();
		List<Map<String, Object>> list = new ArrayList<>();
		certificationBatchDOS.forEach(e->{
			Map<String, Object> map = new HashMap<>();
			map.put("authentication_id",e.getId());
			map.put("certificateName",e.getProfileName());
			map.put("introduce",e.getIntroduce());
			map.put("startTime",e.getStartTime());
			map.put("endTime",e.getEndTime());
			map.put("open_time",e.getOpenTime());
			list.add(map);
		});
		map1.put("list", list);
		return Result.ok(map1);
	}

	@GetMapping("/add")
	@RequiresPermissions("jzweb:informationBatch:add")
	String add(){
	    return "jzweb/informationBatch/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:informationBatch:edit")
	String edit(@PathVariable("id") Long id,Model model){
		InformationBatchDO informationBatch = informationBatchService.selectById(id);
		model.addAttribute("informationBatch", informationBatch);
	    return "jzweb/informationBatch/edit";
	}

	@Log("添加认证批次")
	@ResponseBody
	@PostMapping("/save")
//	@RequiresPermissions("jzweb:informationBatch:add")
	public Result<String> save( InformationBatchCond informationBatch){
		InformationBatchDO informationBatchDO1 = informationBatchService.selectOne(new EntityWrapper<InformationBatchDO>().eq("profile_name", informationBatch.getProfileName()));
		if (informationBatchDO1!=null){
			return Result.fail("存在有相同名字的证书名称");
		}
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		InformationBatchDO informationBatchDO=new InformationBatchDO();
		BeanUtils.copyProperties(informationBatch,informationBatchDO);
		informationBatchDO.setCreaterId(this.getUserId());
		LocalDate start = LocalDate.parse(informationBatch.getStartTime(), formatter);
		LocalDate end = LocalDate.parse(informationBatch.getEndTime(), formatter);
		informationBatchDO.setStartTime(start);
		informationBatchDO.setEndTime(end);
		informationBatchDO.setCreaterDate(new Date());
		informationBatchService.insert(informationBatchDO);
        return Result.ok();
	}

	@Log("修改认证批次")
	@ResponseBody
	@RequestMapping("/update")
//	@RequiresPermissions("jzweb:informationBatch:edit")
	public Result<String>  update( InformationBatchCond informationBatch){
		InformationBatchDO informationBatchDO = informationBatchService.selectById(informationBatch.getId());
		if (!Objects.equals(informationBatch.getProfileName(), informationBatchDO.getProfileName())){
			InformationBatchDO informationBatchDO1 = informationBatchService.selectOne(new EntityWrapper<InformationBatchDO>().eq("profile_name", informationBatch.getProfileName()));
			if (informationBatchDO1!=null){
				return Result.fail("存在有相同名字的证书名称");
			}
		}

		BeanUtils.copyProperties(informationBatch,informationBatchDO);
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		LocalDate start = LocalDate.parse(informationBatch.getStartTime(), formatter);
		LocalDate end = LocalDate.parse(informationBatch.getEndTime(), formatter);
		informationBatchDO.setStartTime(start);
		informationBatchDO.setEndTime(end);
		boolean update = informationBatchService.updateById(informationBatchDO);
		return update ? Result.ok() : Result.fail();
	}

	@Log("删除认证批次")
	@PostMapping( "/remove")
	@ResponseBody
//	@RequiresPermissions("jzweb:informationBatch:remove")
	public Result<String>  remove( Long id){
		informationBatchService.deleteById(id);
        return Result.ok();
	}

	@Log("批量删除认证批次")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:informationBatch:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		informationBatchService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
